package cn.bellychang.aspect;

import cn.bellychang.annotation.HfiDataSource;
import cn.bellychang.config.DataSourceContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @author ChangLiang
 * @date 2020/9/10
 */
@Aspect
@Component
public class DynamicDataSourceAspect {

    @Before("@annotation(ds)")
    public void beforeSwitchDS(JoinPoint point, HfiDataSource ds) {
        String dataSource = ds.value();
        // 切换数据源
        DataSourceContextHolder.setDB(dataSource);
    }

    @After("@annotation(ds)")
    public void afterSwitchDs(JoinPoint point, HfiDataSource ds) {
        DataSourceContextHolder.clearDB();
    }
}
